package com.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.entity.Major;
import com.edu.mapper.MajorMapper;
import com.edu.service.MajorService;
import com.edu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName MajorService
 * @Description
 * @Author W.Jian
 * @Date 2021/5/22 17:48
 **/
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper,Major> implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    /**
     * 根据学校查专业
     * @param schoolId
     * @return
     */
/*    @Override
    public List<Major> listBySchoolId(Long schoolId) {
        if(StringUtils.isEmpty(schoolId)){
            return null;
        }
        return majorMapper.selectBySchoolId(schoolId);
    }*/

//    /**
//     * 给学校添加专业
//     * @param majorRefSubject
//     * @return
//     */
//    @Override
//    public Result insert(MajorRefSubject majorRefSubject) {
//        List<Major> majors = listBySchoolId(majorRefSubject.getSchoolId());
//        if(!CollectionUtils.isEmpty(majors)){
//            return Result.failure("500","该学校已经添加该专业");
//        }
//        int insert = majorRefSubjectMapper.insert(majorRefSubject);
//        if(insert<=0){
//            return Result.failure();
//        }
//        return Result.success();
//    }

    /**
     * 添加专业
     * @param major
     * @return
     */
    @Override
    public Result saveMajor(Major major) {
        if(major.getId()==null){
            HashMap<String, Object> param = new HashMap<>();
            param.put("del_flag",0);
            param.put("school_id", major.getSchoolId());
            param.put("name",major.getName());
            List<Major> majors = majorMapper.selectByMap(param);
            if(!CollectionUtils.isEmpty(majors)){
                return Result.failure("500","添加失败,该专业已经存在！");
            }
            major.setDelFlag(0);
            majorMapper.insert(major);
        }else {
            HashMap<String, Object> param = new HashMap<>();
            param.put("school_id",major.getSchoolId());
            param.put("name",major.getName());
            param.put("del_flag",0);
            List<Major> majors = majorMapper.selectByMap(param);
            if(!CollectionUtils.isEmpty(majors)){
                return Result.failure("500","该学校下面已有该专业");
            }else {
                majorMapper.updateById(major);
            }
        }

        return Result.success();
    }

    @Override
    public Result queryBySchoolId(Integer schoolId) {
        QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
        majorQueryWrapper.eq("school_id", schoolId);
        majorQueryWrapper.eq("del_flag", 0);
        List<Major> majorList = majorMapper.selectList(majorQueryWrapper);
        return Result.success(majorList);
    }

    @Override
    public Result updateDelFlag(Long majorId) {
        Major major = new Major();
        major.setDelFlag(1);
        major.setId(majorId);
        int update = majorMapper.updateById(major);
        return Result.success();
    }

}
